<?php
namespace Modules\Node\Library;

use Modules\Region\Entity\Block as BlockEntity;
use Modules\Node\Entity\Node;
use Phalcon\Di;

class Block
{
    public static function nodeList(BlockEntity $blockEntity)
    {
        $di = Di::getDefault();
        $data = $blockEntity->getData();
        $query = [
            'sort' => [],
            'limit' => $data['limit'],
            'skip' => 0,
        ];
        if (isset($data['sort'])) {
            switch ($data['sort']) {
                case 'hot':
                    $query['sort'] = [
                        'socialize' => -1,
                        'changed' => -1,
                    ];
                    break;
                case 'new':
                    $query['sort'] = [
                        'changed' => -1,
                    ];
                    break;
                case 'browse':
                    $query['sort'] = [
                        'browse' => -1,
                        'changed' => -1,
                    ];
                    break;
                case 'comment':
                    $query['sort'] = [
                        'comment' => -1,
                        'changed' => -1,
                    ];
                    break;
            }
        }
        if (isset($data['nodeContentModel'])) {
            if (is_array($data['nodeContentModel'])) {
                $query['conditions']['contentModel'] = ['$in' => array_values($data['nodeContentModel'])];
            } else {
                $query['conditions']['contentModel'] = $data['nodeContentModel'];
            }
        }
        if (isset($data['state'])) {
            $query['conditions']['state'] = intval($data['state']);
        }
        if (isset($data['term']) && !empty($data['term'])) {
            $nodeFields = $di->getShared('entityManager')->field('node');
            $termFieldList = [];
            foreach (formFieldInit($nodeFields) as $fieldName => $field) {
                if (isset($field['term'])) {
                    $termFieldList[$fieldName] = $fieldName;
                }
            }

            foreach ($termFieldList as $field) {
                $query['conditions'][$field] = [
                    '$in' => $data['term']
                ];
            }
        }

        $nodeList = $di->getShared('entityManager')->find('node', $query);
        $output = [
            '#module' => 'node',
            'data' => $blockEntity,
            'nodeList' => $nodeList,
        ];
        if(isset($data['nodeContentModel']) && count($data['nodeContentModel']) == 1){
            $nodeContentModel = end($data['nodeContentModel']);
            $output['#templates'] = [
                'block-nodeList',
                'block-node-' . $data['layout'],
                'block-node-' . $nodeContentModel,
                'block-nodeList-' . $data['layout'],
                'block-node-' . $nodeContentModel.'-'.$data['layout'],
                'block-' . $blockEntity->getId()
            ];
        }else{
            $output['#templates'] = [
                'block-nodeList',
                'block-node-' . $data['layout'],
                'block-nodeList-' . $data['layout'],
                'block-' . $blockEntity->getId()
            ];
        }
        return $output;
    }

    public static function nodeRelated(BlockEntity $blockEntity)
    {
        $data = $blockEntity->getData();
        $query = [
            'sort' => [
                'changed' => -1,
                'browse' => -1
            ],
            'limit' => $data['limit'],
        ];

        if (isNode()) {
            $node = getNode();
            if($node instanceof Node) {
                $termFieldList = [];
                foreach (formFieldInit($node->getField()) as $fieldName => $field) {
                    if (isset($field['term']) && isset($node->{$fieldName}) && !empty($node->{$fieldName})) {
                        $termFieldList[$fieldName] = $fieldName;
                        $query['conditions']['$or'][][$fieldName] = [
                            '$in' => array_values($node->{$fieldName})
                        ];
                    }
                }
            }
        }

        if (isset($data['sort'])) {
            switch ($data['sort']) {
                case 'hot':
                    $query['sort'] = [
                        'socialize' => -1,
                        'changed' => -1,
                    ];
                    break;
                case 'new':
                    $query['sort'] = [
                        'changed' => -1,
                    ];
                    break;
                case 'browse':
                    $query['sort'] = [
                        'browse' => -1,
                        'changed' => -1,
                    ];
                    break;
                case 'comment':
                    $query['sort'] = [
                        'comment' => -1,
                        'changed' => -1,
                    ];
                    break;
            }
        }
        $nodeList = Di::getDefault()->getShared('entityManager')->find('node', $query);

        return [
            '#templates' => [
                'block-relatedNode',
                'block-relatedNode-' . $data['layout'],
                'block-' . $blockEntity->getId()
            ],
            '#module' => 'node',
            'data' => $blockEntity,
            'nodeList' => $nodeList,
        ];
    }
}